package com.foronenet.mud;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

public class MudUdpServerDemo {
    public static void main(String[] args) throws IOException {
//        DatagramSocket socket = new DatagramSocket(8080, InetAddress.getByName("192.168.0.3"));
        DatagramSocket socket = new DatagramSocket(8080);
//        socket.setReuseAddress(true);
//        socket.setReceiveBufferSize(1024*2048);

        while (true) {
            byte[] buffer = new byte[1024*64];
            DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
            socket.receive(packet);
            int len = packet.getLength();
//            String str = new String(, 0, len);
//            System.out.println(packet.getAddress()+":"+packet.getPort()+"=>"+len+"=>"+str);
            int c1 = ParseUtils.bytesToShort(packet.getData(), 0);
            int c2 = ParseUtils.bytesToShort(packet.getData(), 2);
            int c3 = ParseUtils.bytesToShort(packet.getData(), 4);
            int c4 = ParseUtils.bytesToShort(packet.getData(), 6);
            int canId = ParseUtils.bytesToShort(packet.getData(), 8);
            System.out.println(packet.getAddress()+":"+packet.getPort()+"="+len+"=>"+
                    "boxId:"+ParseUtils.getBoxId(canId)+",canId:"+canId+",c1:"+c1+",c2:"+c2+",c3:"+c3+",c4:"+c4);

            if(canId == 4){
                System.out.println("V:"+ParseUtils.sv2v(c3));
            }
            if(canId == 7){
                System.out.println("V:"+ParseUtils.sv2v(c1));
                System.out.println("mA:"+ParseUtils.sv2a(c1));
            }

        }
    }


}
